<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./jquery.js"></script>
  </head>
  <body>
    <select name="" id="province">
      <option value=""></option>
    </select>
    <select name="" id="city">
      <option value="">请选择市</option>
    </select>
    <select name="" id="area">
      <option value="">请选择区县</option>
    </select>
  </body>
  <script src="./city.js"></script>
  <script>
    var proviceIndex
    $(function () {
      //显示省的列表
      var strProvince = `<option value="">请选择省</option>`
      for (let i in citys) {
        strProvince += `<option value="${i}">${citys[i].name}</option>`
      }
      $('#province').html(strProvince)
      //省改变事件 修改市的下拉列表
      $('#province').change(function () {
        proviceIndex = $(this).val()
        var strCitys = `<option value="">请选择市</option>`
        for (let i in citys[proviceIndex].city) {
          strCitys += `<option value="${i}">${citys[proviceIndex].city[i].name}</option>`
        }
        $('#city').html(strCitys)
        $('#area').html(`<option value="">请选择区县</option>`)
      })
      //市改变事件 修改区县的下拉列表
      $('#city').change(function () {
        var cityIndex = $(this).val()
        var tmpArr = citys[proviceIndex].city[cityIndex].area
        var strArea = `<option value="">请选择区县</option>`
        for (let i in tmpArr) {
          strArea += `<option value="">${tmpArr[i]}</option>`
        }
        $('#area').html(strArea)
      })
    })
  </script>
</html>
